package Action.Sys;

import Action.BackgroundThread.GcThread;
import Bean.Log.SystemLogEntity;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;

import java.util.LinkedList;
import java.util.List;

public class SettingInfo extends ActionSupport {
    long UserNum;
    long BookNum;
    long SysLog;
    long UserLog;
    long orderLog;
    long CommentSize;
    long LoanrecordSize;
    String cacheSize;
    String databaseSize;
    String AllSize;//系统占用的总的空间
    long clear;
    LinkedList<String> sysLogs=new LinkedList<>();
    LinkedList<String> userLogs=new LinkedList<>();
    LinkedList<String> bookLogs=new LinkedList<>();

    //可配置的系统设置--本次启动有效
    private int LoanBook_Max_Num;
    private int LoanBookNum_Max_Num;//借书最大数量
    private int LoanBook_Max_day;
    private  int LoanBook_Min_day;
    private  long SYS_min_cache_time;
    private  long SYS_cache_Max_time;
    private  long SYS_cache_MaxSize;
    private  long SYS_cache_Model;
    Session session;
//static {//系统类加载时启动
//    GcThread.getInstance().StartGc();
//}
    public String SystemInfo(){
        //垃圾清理
        GcThread.getInstance().StartGc();
        LoanBook_Max_Num = Setting.LoanBook_Max_Num;
        LoanBookNum_Max_Num = Setting.LoanBookNum_Max_Num;
        LoanBook_Max_day = Setting.LoanBook_Max_day;
        LoanBook_Min_day = Setting.LoanBook_Min_day;
        SYS_min_cache_time = Setting.SYS_min_cache_time;
        SYS_cache_Max_time = Setting.SYS_cache_Max_time;
        SYS_cache_MaxSize = Setting.SYS_cache_MaxSize;
        SYS_cache_Model=Setting.SYS_cache_Model;
        try {
            session = HibernateTools.getSession();
            UserNum= (Long) session.createQuery("select count (*) from UserinfoEntity ").list().get(0);
            BookNum= (Long) session.createQuery("select count (*) from BookInfoEntity ").list().get(0);
            SysLog= (Long) session.createQuery("select count (*) from SystemLogEntity ").list().get(0);
            SysLog+= (Long) session.createQuery("select count (*) from HistoricalData ").list().get(0);
            UserLog= (Long) session.createQuery("select count (*) from UserLogEntity ").list().get(0);
            UserLog+=(long)session.createQuery("select count (*) from OrderLog").list().get(0);
            LoanrecordSize= (Long) session.createQuery("select count (*) from LoanBookLogRecord ").list().get(0);
            CommentSize= (Long) session.createQuery("select count (*) from BookCommentEntity ").list().get(0);
            orderLog= (Long) session.createQuery("select count (*) from OrderLog ").list().get(0);
        }catch (Exception e){
            addActionError("服务器异常");
            System.err.println(e.getMessage());
        }

        try {
            cacheSize=String.format("%.1f",Setting.cache_size());
        } catch (Exception e) {
            addActionError("缓存没有正常初始化");
            cacheSize="无法获取缓存状态";
            System.out.println("缓存没有正常初始化");
        }
        try {
            databaseSize=String.format("%.1f",Setting.database_size());
            AllSize=String.format("%.1f",Setting.All_size());
            System.out.println(AllSize);
        } catch (Exception e) {
            databaseSize="无法获知数据库状态";
            System.out.println("数据库路径不正确");
        }
        build_logs();
        HibernateTools.commit(session);
        return SUCCESS;
    }

    private void build_logs() {
        List<SystemLogEntity> systemLogEntities=session.createQuery("from SystemLogEntity order by id desc ").getResultList();
        for (SystemLogEntity systemLogEntity : systemLogEntities) {
            //用户添加,删除,修改
            if (systemLogEntity.getFlag().equals(Setting.SystemLog_flag_AddUser)||systemLogEntity.getFlag().equals(Setting.SystemLog_flag_DelUser)||systemLogEntity.getFlag().equals(Setting.SystemLog_flag_AltUserData))
                userLogs.addLast(systemLogEntity.BuildLog());
            else if (systemLogEntity.getFlag().equals(Setting.SystemLog_flag_AddBook)||systemLogEntity.getFlag().equals(Setting.SystemLog_flag_DelBookData)||systemLogEntity.getFlag().equals(Setting.SystemLog_flag_AltBookData))
                bookLogs.addLast(systemLogEntity.BuildLog());
            else if (systemLogEntity.getUserId()==-1){
                //系统内部--缓存
                sysLogs.addLast(systemLogEntity.BuildLog());
            }
        }

    }

    public String cache_clear(){
        clearErrorsAndMessages();
        try {
            clear=Setting.clear_cache();
        } catch (Exception e) {
           addActionError("垃圾清理出现异常请检查系统日志");
        }
        if (clear>0)
            addActionMessage("手动垃圾清理结果:"+clear+"MB");
        else {
            addActionError("垃圾清理失败,没有可以释放的资源");
        }
        return SystemInfo();
    }
    public String database_clear(){
        clearErrorsAndMessages();
        String res=SUCCESS;
        try {
            addActionMessage(Setting.database_clear());
        } catch (Exception e){
           e.printStackTrace();
            res=ERROR;
        }
        SystemInfo();

        return res;
    }
    public String update_Setting(){
        clearErrorsAndMessages();
         Setting.LoanBook_Max_Num=LoanBook_Max_Num;
         Setting.LoanBookNum_Max_Num=LoanBookNum_Max_Num;
        Setting.LoanBook_Max_day=LoanBook_Max_day;
        Setting.LoanBook_Min_day=LoanBook_Min_day;
        Setting.SYS_min_cache_time=SYS_min_cache_time;
        Setting.SYS_cache_Max_time=SYS_cache_Max_time;
        Setting.SYS_cache_MaxSize=SYS_cache_MaxSize;
        Setting.SYS_cache_Model=SYS_cache_Model;

        addActionMessage("修改成功");
        return SystemInfo();
    }

    public long getUserNum() {
        return UserNum;
    }

    public long getBookNum() {
        return BookNum;
    }

    public long getSysLog() {
        return SysLog;
    }

    public long getUserLog() {
        return UserLog;
    }

    public String getCacheSize() {
        return cacheSize;
    }

    public LinkedList<String> getSysLogs() {
        return sysLogs;
    }

    public long getOrderLog() {
        return orderLog;
    }

    public int getLoanBookNum_Max_Num() {
        return LoanBookNum_Max_Num;
    }

    public LinkedList<String> getUserLogs() {
        return userLogs;
    }

    public LinkedList<String> getBookLogs() {
        return bookLogs;
    }

    public long getClear() {
        return clear;
    }

    public int getLoanBook_Max_Num() {
        return LoanBook_Max_Num;
    }

    public int getLoanBook_Max_day() {
        return LoanBook_Max_day;
    }

    public String getDatabaseSize() {
        return databaseSize;
    }

    public long getCommentSize() {
        return CommentSize;
    }

    public long getLoanrecordSize() {
        return LoanrecordSize;
    }

    public int getLoanBook_Min_day() {
        return LoanBook_Min_day;
    }

    public long getSYS_min_cache_time() {
        return SYS_min_cache_time;
    }

    public long getSYS_cache_Max_time() {
        return SYS_cache_Max_time;
    }

    public void setSYS_cache_Max_time(long SYS_cache_Max_time) {
        this.SYS_cache_Max_time = SYS_cache_Max_time;
    }

    public long getSYS_cache_MaxSize() {
        return SYS_cache_MaxSize;
    }

    public void setLoanBook_Max_Num(int loanBook_Max_Num) {
        LoanBook_Max_Num = loanBook_Max_Num;
    }

    public void setLoanBook_Max_day(int loanBook_Max_day) {
        LoanBook_Max_day = loanBook_Max_day;
    }

    public void setLoanBook_Min_day(int loanBook_Min_day) {
        LoanBook_Min_day = loanBook_Min_day;
    }

    public void setSYS_min_cache_time(long SYS_min_cache_time) {
        this.SYS_min_cache_time = SYS_min_cache_time;
    }


    public void setSYS_cache_MaxSize(long SYS_cache_MaxSize) {
        this.SYS_cache_MaxSize = SYS_cache_MaxSize;
    }

    public long getSYS_cache_Model() {
        return SYS_cache_Model;
    }

    public void setSYS_cache_Model(long SYS_cache_Model) {
        this.SYS_cache_Model = SYS_cache_Model;
    }

    public String getAllSize() {
        return AllSize;
    }

    public void setAllSize(String allSize) {
        AllSize = allSize;
    }
}
